define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'classes/student/index' + location.search,
                    add_url: 'classes/student/add',
                    edit_url: 'classes/student/edit',
                    del_url: 'classes/student/del',
                    multi_url: 'classes/student/multi',
                    dragsort_url: '',
                    table: 'classes_student',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'asc',
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'classes_id',
                            title: __('Classes'),
                            visible: false,
                            addclass: 'selectpage',
                            extend: 'data-source="classes/classes/index" data-field="title"',
                            operate: '=',
                            formatter: Table.api.formatter.search
                        },
                        {
                            field: 'admin_id',
                            title: __('SchoolName'),
                            visible: false,
                            addclass: 'selectpage',
                            extend: 'data-source="classes/school/index" data-primary-key="admin_id" data-field="school_name"',
                            operate: '=',
                            formatter: Table.api.formatter.search,
                            searchable: Config.isSuperAdmin
                        },
                        {field: 'id', sortable: true, title: __('Id')},
                        {field: 'school.school_name', title: __('SchoolName'), searchable: false, visible: Config.isSuperAdmin && Config.authVersion == 2, formatter:function(value){ return value ?? '本部'; }},
                        {field: 'student_number', sortable: true, title: __('studentNumber')},
                        {field: 'student_avatar', sortable: true, title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'realname', sortable: true, title: __('Realname')},
                        {field: 'student_gender', sortable: true, title: __('Gender'), formatter: function(value, row, index){
                                return value == 0 ? '男' : (value == 1 ? '女' : '未知')
                            }},
                        {
                            field: 'buttons',title: __('Classes'), table: table,
                            buttons: [{
                                name: 'classes',
                                text: __('View'),
                                operate: false,
                                classname: 'btn btn-xs btn-success btn-dialog',
                                icon: 'fa fa-eye',
                                url: 'classes/classes/index?students_id={ids}'
                            }],
                            visible: !Config.classes_id,
                            formatter: Table.api.formatter.buttons
                        },
                        // {field: 'classes.title', sortable: true, title: __('Classes'), searchable: false},
                        {field: 'familymember', title: __('Familymember'), formatter: function (value, row, index) {
                                var v = '';

                                if(!value || value.length == 0){
                                    return '';
                                }
                                for(i=0; i<value.length; i++){
                                    v += value[i].callname + '：' + (value[i].profile ? value[i].profile.realname : '') + ' ' + (value[i].profile ? value[i].profile.mobile : '') + '<br/>'
                                }
                                return v;
                            }},
                        {field: 'status', title: __('Status'), searchList: {"1": __('通过'), "0": __('待审批')}, formatter: Table.api.formatter.toggle, visible:Config.classes_id},
                        {field: 'updatetime', sortable: true, title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                search: true,
                commonSearch: true
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            /*if(Config.isSuperAdmin){
                schoolChange();
            }
            Controller.api.bindevent();
            $("#c-admin_id").change(function(){
                $('#c-classes_id').selectPageClear();
                schoolChange();
            });*/

            $("#c-classes_id").data("params", function(){
                return {"custom": {"admin_id": $("#c-admin_id").val(),'status':'normal' }};
            });
            Controller.api.bindevent();

        },
        edit: function () {
            /*if(Config.isSuperAdmin){
                schoolChange();
            }
            $("#c-admin_id").change(function(){
                $('#c-classes_id').selectPageClear();
                schoolChange();
            });*/

            $("#c-classes_id").data("params", function(){
                return {"custom": {"admin_id": $("#c-admin_id").val(),'status':'normal' }};
            });
            Controller.api.bindevent();

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    function schoolChange(){
        $admin_id = $("#c-admin_id").val();
        $('.fachoose').each(function(){
            $(this).data('admin-id', $admin_id);
        });
        //console.log($("#c-classes_id").attr("data-field"));
        $("#c-classes_id").data("params", function(){
            return {"custom[admin_id]": $admin_id, "custom[status]": 'normal'};
        });
    }
    return Controller;
});